﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WEB.MVClient.Models;
using WEB.DAL.Models;

namespace WEB.MVClient.Code
{
    public static class CandidateConverter
    {
        private static CandidateModel ConvertToModel(Candidate candidate)
        {
            CandidateModel model = new CandidateModel();

            model.ID = candidate.Id;

            model.LastName = candidate.LastName;

            if (candidate.DateOfBirthDay.HasValue)
            {
                model.Age = (int)DateTime.Today.Subtract(candidate.DateOfBirthDay.Value).Days / 365;

                model.DateOfBirthday = candidate.DateOfBirthDay;
            }

            model.HasResume = candidate.HasResume;

            model.Exprience = candidate.Exprience;

            foreach (Candidate_Positions cp in candidate.Candidate_Positions)
            {
                model.Positions += string.Format("{0};", cp.Position.PositionName);
            }

            model.Territories = candidate.Territories;

            model.Car = candidate.Car;

            return model;
        }

        public static CandidateListModel Convert(IEnumerable<Candidate> candidates)
        {
            List<CandidateModel> list = new List<CandidateModel>();
            
            foreach (Candidate candidate in candidates)
            {
                list.Add(ConvertToModel(candidate));
            }

            CandidateListModel listModel = new CandidateListModel();

            listModel.Candidates = from n in list
                                   select n;

            return listModel;
        }

    }
}